﻿using HamcoWcs.Entitys.DeviceErrors;
using HamcoWcs.Entitys.MesMsgQueues;
using HamcoWcs.Models.BaseConfigs;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using System;
using System.Collections.Generic;
using System.IO;

namespace HamcoWcs.Models.Common
{


    public class GlobalValue
    {
        /// <summary>
        /// 
        /// </summary>
        static GlobalValue()
        {
            G_CONFIG = new GlobalValue();
            RootPath = AppDomain.CurrentDomain.BaseDirectory;
            LogPath = Path.Combine(RootPath, "logs");
            PlaybackLogFolder = Path.Combine(LogPath, "playback_logs");
            CommunicateLogFolder = Path.Combine(LogPath, "communication_logs");
            TaskLogFolder = Path.Combine(LogPath, "main_task_bak");
            MilageLogFolder = Path.Combine(LogPath, "milage_logs");
            if (!Directory.Exists(LogPath))
            {
                Directory.CreateDirectory(LogPath);
                if (!Directory.Exists(PlaybackLogFolder))
                    Directory.CreateDirectory(PlaybackLogFolder);
                if (!Directory.Exists(CommunicateLogFolder))
                    Directory.CreateDirectory(CommunicateLogFolder);
                if (!Directory.Exists(TaskLogFolder))
                    Directory.CreateDirectory(TaskLogFolder);
                if (!Directory.Exists(MilageLogFolder))
                    Directory.CreateDirectory(MilageLogFolder);
            }
        }

        #region [Path]

        public static string RootPath { get; }
        public static string LogPath { get; }
        public static string PlaybackLogFolder { get; }
        public static string TaskLogFolder { get; }
        public static string CommunicateLogFolder { get; }

        public static string MilageLogFolder { get; }

        #endregion

        public static bool WCSConnectState { get; set; } = true;
        public static Queue<string> TeachShelfNames { get; set; } = new Queue<string>();

        public static int MesCount { get; set; }

        public static bool IsError => ErrorFlag1 | ErrorFlag2 | ErrorFlag3 | ErrorFlag4 | ErrorFlag5 | ErrorFlag6 | ErrorFlag7;
        public static bool ErrorFlag1 { get; set; } = false;
        public static bool ErrorFlag2 { get; set; } = false;
        public static bool ErrorFlag3 { get; set; } = false;
        public static bool ErrorFlag4 { get; set; } = false;
        public static bool ErrorFlag5 { get; set; } = false;
        public static bool ErrorFlag6 { get; set; } = false;
        public static bool ErrorFlag7 { get; set; } = false;
        //public static List<DeviceErrorQueue> ERROR_BUFFER { get; set; }         //设备报错信息缓存

        //public static Dictionary<string, MachineDto> MACHINE_LIST;
        public static IServiceProvider Services { get; set; }                   //依赖容器
        public static int CV_RECV_MSG_SIZE = 54;                                //输送线接收消息长度
        public static int CV_SEND_MSG_SIZE = 34;                                //输送线发送消息长度
        public static int SRM_RECV_MSG_SIZE = 54;                               //堆垛机接收消息长度
        public static int SRM_SEND_MSG_SIZE = 46;                               //堆垛机发送消息长度
        public static string PALLET_PLC_TYPE = "Pcon";                          //托盘线PLC类型
        public static string SRM_PLC_TYPE = "Srm";                              //堆垛机PLC类型
        public static string BOX_PLC_TYPE = "Bcon";                             //箱线PLC类型
        public static string MES_TRANSACTIONID_FORMAT = "yyyyMMddHHmmssffffff"; //MES消息时间样式
        public static string MES_SYSTEMTIME_FORMAT = "yyyyMMddHHmmss";          //MES系统时间样式
        public static string DEFAULT_TASK_PRIORITY = "60";                      //默认任务优先级，WCS手动下任务
        public static string DEFAULT_INVENTORY_TASK_PRIORITY = "30";            //默认盘点任务优先级
        public static int SERVER_TIME_DIFF_THRESHOLD = 60 * 5;                  //WCS服务器和MES服务器时间差阈值(当前为5min)
        public static int CJSTW100_MID_BAY = 21;                                //CJSTW100中间列
        public static int CJSTW200_MID_BAY = 29;                                //CJSTW200中间列
        public static int SRM_MSG_REPEAT_TIME = 5;                              //堆垛机判断消息是否重复时间
        public static int VALID_READ_LENGTH = 96;                               //DB103读取托盘号和箱号长度


        public static Queue<GetSrmTeachModel> SrmTeachRequestQueue { get; set; } = new Queue<GetSrmTeachModel>();

        // ==================================默认值==================================
        // 默认任务优先级自增间隔
        private static int DEFAULT_TASK_PRIORITY_INC_INTERVAL = 5;
        // 默认任务超时阈值(标红)
        private static int DEFAULT_TASK_TIMEOUT_THRESHOLD = 30;
        // 默认重新计算路径时间
        private static int DEFAUT_ERROR_REROUTE_TIME = 10;

        // ==================================默认值==================================

        public static GlobalValue G_CONFIG { get; set; }



        private int? _TaskTimeoutThreshold;
        private int? _TaskPriorityIncInterval;
        private int? _ErrorRerouteTime;


        public int TaskTimeoutThreshold
        {
            get => _TaskTimeoutThreshold ?? DEFAULT_TASK_TIMEOUT_THRESHOLD;
            set => _TaskTimeoutThreshold = value;
        }


        public int TaskPriorityIncInterval
        {
            get => _TaskPriorityIncInterval ?? DEFAULT_TASK_PRIORITY_INC_INTERVAL;
            set => _TaskPriorityIncInterval = value;
        }



        public int ErrorRerouteTime
        {
            get => _ErrorRerouteTime ?? DEFAUT_ERROR_REROUTE_TIME;
            set => _ErrorRerouteTime = value;
        }


        /// <summary>
        /// Pcon全局参数
        /// </summary>
        public static Dictionary<string, GlobalConfigModel> PconGlobalConfigs { get; } = new Dictionary<string, GlobalConfigModel>();
        /// <summary>
        /// 设置PLC时间间隔（s）
        /// </summary>
        public static int SetPLCDateTimeInterval { get; } = 3600;
        /// <summary>
        /// 设置PLC时间计数
        /// </summary>
        public static int SetPLCDateTimeTick { get; set; } = 3600;
        /// <summary>
        /// 特殊节点
        /// </summary>
        public static List<string> SpecialNodes { get; } = new List<string>
        {
            "CJRGV100",
            "CJRGV200",
            //"CJMIFB00",
            //"CJMIFC00",
        };
        /// <summary>
        /// 提升机节点
        /// </summary>
        public static List<string> LeftNodes { get; } = new List<string>
        {
            "CJMIFB00",
            "CJMIFC00",
        };
        /// <summary>
        /// 成品入库节点
        /// </summary>
        public static List<string> ProductInNodes { get; } = new List<string>
        {
            "CJMCV700",
            "CJMCV300",
        };
        /// <summary>
        /// Pcon初始化标志
        /// </summary>
        public static bool PconInitFlag { get; set; } = false;
        /// <summary>
        /// Pcon路径初始化计数
        /// </summary>
        public static int PconRouteTick { get; set; } = 0;
        /// <summary>
        /// Pcon路径初始化阈值
        /// </summary>
        public static int PconRouteTickThreshold { get; } = 5;

        // RGV定位信息对应的偏移量
        public static Dictionary<string, int> RgvPositionDic { get; } = new Dictionary<string, int>()
        {
            // L10
            { "CJVCV500", 108 },
            { "CJVCV600", 112 },
            { "CJVCV300", 116 },
            { "CJVCV400", 120 },
            // L20
            { "CJVCVB00", 124 },
            { "CJVCVC00", 128 },
            { "CJMIFA00", 132 },
            { "CJMIF900", 136 },
            { "CJMIF800", 140 },
            { "CJMIF700", 144 },
            { "CJBUF104", 148 },
            { "CJBUF103", 152 },
            { "CJBUF102", 156 },
            { "CJBUF101", 160 },
            { "CJMIF600", 164 },
            { "CJMIF500", 168 },
            { "CJMIF400", 172 },
            { "CJMIF300", 176 },
            { "CJMIF200", 180 },
            { "CJMIF100", 818 },
        };

        public static Dictionary<string, int> SrmHmiSpeedDic { get; } = new Dictionary<string, int>()
        {
            { "CJSTW100C1XSPEED", 5180 },
            { "CJSTW100C1YSPEED", 5184 },
            { "CJSTW100C1ZSPEED", 5182 },
            { "CJSTW100C2XSPEED", 5580 },
            { "CJSTW100C2YSPEED", 5584 },
            { "CJSTW100C2ZSPEED", 5582 },
                         
            { "CJSTW200C1XSPEED", 5180 },
            { "CJSTW200C1YSPEED", 5184 },
            { "CJSTW200C1ZSPEED", 5182 },
            { "CJSTW200C2XSPEED", 5580 },
            { "CJSTW200C2YSPEED", 5584 },
            { "CJSTW200C2ZSPEED", 5582 },
        };
        /// <summary>
        /// Srm 全局参数
        /// </summary>
        public static Dictionary<string, GlobalConfigModel> SrmGlobalConfigs { get; } = new Dictionary<string, GlobalConfigModel>();
        /// <summary>
        /// 堆垛机无任务有货下任务阈值
        /// </summary>
        public static int DefaultSrmNoTaskIsGoodsTimeThreshold { get; } = 20 * 60;
        /// <summary>
        /// 堆垛机禁用/启用标志
        /// </summary>
        public static Dictionary<string, int> DIC_SRM_DISABLED_FLAG { get; set; } =
        new Dictionary<string, int>()
        {
            {"CJSTW100-C1",0 },
            {"CJSTW100-C2",0 },
            {"CJSTW200-C1",0 },
            {"CJSTW200-C2",0 },
        };
        /// <summary>
        /// Srm参数地址
        /// </summary>
        public static List<int> SrmAddrs => new List<int>()
        {
            5161, 5162,
            5186, 5187, 5188,
            5189, 5190, 5191,
            5192, 5193, 5194,
        };

        /// <summary>
        /// 获取Teach信息队列
        /// </summary>
        public static Dictionary<string, string> SrmTeachQueue { get; set; } = new Dictionary<string, string>()
        {
            { "CJSTW100-C1",""},
            { "CJSTW100-C2",""},
            { "CJSTW200-C1",""},
            { "CJSTW200-C2",""},
        };



        public static List<string> RouteNeedChangeRouteNode => new List<string>()
        {
            "CJVCV300","CJVCV400","CJVCV500","CJVCV600","CJVCV700","CJVCV800",
            "CJRGV200",
            "CJMIFB00","CJMIFC00","CJVCVB00","CJVCVC00"
        };

        public static bool MesInitFlag { get; set; } = false;
        public static bool MesOnlineFlag { get; set; } = false;
        public static int MesConnectCount { get; set; } = 0;
        public static int MesTimeTick { get; set; } = 0;

        public static EnumMesInitState MesInitState { get; set; } = EnumMesInitState.None;

        public static bool MesHBEnabled { get; set; } = true;
        public static int MesHbInterval { get; set; } = 10;

        public static List<MesMsgQueue> MesMsgQueueBuffer { get; set; } = new List<MesMsgQueue>();

        /// <summary>
        /// MESOther消息名
        /// </summary>
        public static List<string> MesOtherMsgNames => new List<string>()
        {
            "MachineControlStateChanged",
            "MachineStateChanged",
            "PortStateChanged",
            "InventoryPalletDataRequest",
            "InventoryZoneDataReport",
            "MachineModeChangeRequest",
            "AreYouThereRequest",
            "PortTransferStateChanged",
        };

        public static List<string> AlarmDivceName => new List<string>()
        {
            "CJSTW100","CJSTW100Z1","CJSTW100Z2","CJSTW100Z3","CJSTW100Z4",
            "CJSTW100Z5","CJSTW200","CJSTW200Z1","CJSTW200Z2","CJSTW200Z3",
            "CJSTW200Z4","CJSTW200Z5","CJMCV400","CJMCV500","CJMCV600","CJMIF400",
            "CJMIF500","CJMIF600","CJMCV300","CJMCV700","CJMIF300",
            "CJMIF700","CJRCV100","CJRCV200","CJRCV300","CJRCV400",
            "CJMIFB00","CJMIFB00L2","CJMIFC00","CJMIFC00L2","CJRGV100",
            "CJRGV200","CJSTW100C1","CJSTW100C2","CJSTW200C1","CJSTW200C2",
            "CJVCV100","CJVCV200","CJVCV300","CJVCV400","CJVCV500",
            "CJVCV600","CJVCV700","CJVCV800","CJVCVB00","CJVCVC00",
            "CJBUF100","CJBUF101","CJBUF102","CJBUF103","CJBUF104",
            "CJMIF100","CJMIF200", "CJMIF800","CJMIF900","CJMCVA00",
            "CJMCV100","CJMCV200","CJMCV800","CJMCV900","CJMIFA00",
        };
    }
}
